package cn.ruituo.adv8.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class MyAdvanceTalkServer {
    public static void main(String[] args) throws IOException {
        ServerSocket st=new ServerSocket(8881);
        System.out.println("服务启动成功！");
        while(true){
            final Socket s=st.accept();
            new ChatThread(s).start();
        }
    }
}

class ChatThread extends Thread{
    private Socket s;
    public ChatThread(Socket s){
        this.s=s;
    }
    @Override
    public void run() {
        System.out.println(s+"连接成功了。。。");
        System.out.println(s.getInetAddress()+","+s.getPort());
        try {
            InputStream in=s.getInputStream();
            OutputStream out=s.getOutputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            PrintWriter pw=new PrintWriter(out,true);
            while (true){
                String ask=br.readLine();// \n
                System.out.println("收到"+s+"发来的信息："+ask);
                pw.println("你说的："+ask+"朕知道了");
                if(ask.equals("exit"))
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
                System.out.println(s+"退出了。。。");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}